package com.store.service.impl;

import com.store.dto.request.ProductRequest;
import com.store.entity.Product;
import com.store.mapper.ProductMapper;
import com.store.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> getList(Integer page, Integer pageSize, String keyword, Long categoryId) {
        // 验证分页参数
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        
        int offset = (page - 1) * pageSize;
        if (keyword != null && !keyword.trim().isEmpty()) {
            return productMapper.selectListByKeyword(keyword, offset, pageSize);
        }
        if (categoryId != null) {
            return productMapper.selectListByCategory(categoryId, offset, pageSize);
        }
        return productMapper.selectList(offset, pageSize);
    }

    @Override
    public Product findById(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    @Transactional
    public Product create(ProductRequest request) {
        // 参数验证
        if (request == null) {
            throw new RuntimeException("请求参数不能为空");
        }
        if (request.getProductName() == null || request.getProductName().trim().isEmpty()) {
            throw new RuntimeException("商品名称不能为空");
        }
        if (request.getProductPrice() == null) {
            throw new RuntimeException("商品价格不能为空");
        }
        if (request.getCategoryId() == null) {
            throw new RuntimeException("商品分类不能为空");
        }
        if (request.getStock() == null) {
            throw new RuntimeException("商品库存不能为空");
        }
        
        Product product = new Product();
        product.setProductName(request.getProductName().trim());
        product.setDescription(request.getDescription());
        product.setProductPrice(request.getProductPrice());
        product.setStock(request.getStock());
        product.setCategoryId(request.getCategoryId());
        product.setImageUrl(request.getImageUrl());
        
        productMapper.insert(product);
        return product;
    }

    @Override
    @Transactional
    public Product update(Long id, ProductRequest request) {
        Product product = productMapper.selectById(id);
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }
        
        product.setProductName(request.getProductName());
        product.setDescription(request.getDescription());
        product.setProductPrice(request.getProductPrice());
        product.setStock(request.getStock());
        product.setCategoryId(request.getCategoryId());
        product.setImageUrl(request.getImageUrl());
        
        productMapper.update(product);
        return product;
    }

    @Override
    @Transactional
    public void delete(Long id) {
        productMapper.deleteById(id);
    }

    @Override
    public boolean existsById(Long id) {
        return productMapper.selectById(id) != null;
    }

    @Override
    public Product getTopSalesProduct() {
        return productMapper.selectTopSalesProduct();
    }

    @Override
    public BigDecimal getProductSales(Long id) {
        return productMapper.selectProductSales(id);
    }

    @Override
    public List<String> getAllCategories() {
        return productMapper.selectAllCategories();
    }

    @Override
    public List<Product> getByCategory(Long categoryId, Integer page, Integer pageSize) {
        // 验证分页参数
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        
        int offset = (page - 1) * pageSize;
        return productMapper.selectListByCategory(categoryId, offset, pageSize);
    }

    @Override
    public List<Product> search(String keyword, Integer page, Integer pageSize) {
        // 验证分页参数
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        
        int offset = (page - 1) * pageSize;
        return productMapper.selectListByKeyword(keyword, offset, pageSize);
    }

    @Override
    @Transactional
    public void updateStock(Long id, Integer stock) {
        Product product = productMapper.selectById(id);
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }
        
        if (stock < 0) {
            throw new RuntimeException("库存不能为负数");
        }
        
        product.setStock(stock);
        productMapper.update(product);
    }

    @Override
    public List<Product> selectList(int page, int pageSize) {
        // 验证分页参数
        if (page < 1) {
            page = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        
        int offset = (page - 1) * pageSize;
        return productMapper.selectList(offset, pageSize);
    }

    @Override
    public Product selectById(Long productId) {
        return productMapper.selectById(productId);
    }

    @Override
    public List<Product> selectByCategoryId(Long categoryId) {
        return productMapper.selectByCategoryId(categoryId);
    }

    @Override
    @Transactional
    public int insert(Product product) {
        return productMapper.insert(product);
    }

    @Override
    @Transactional
    public int update(Product product) {
        return productMapper.update(product);
    }

    @Override
    @Transactional
    public int deleteById(Long productId) {
        return productMapper.deleteById(productId);
    }

    @Override
    public long count() {
        return productMapper.count();
    }

    @Override
    public long countByCategoryId(Long categoryId) {
        return productMapper.countByCategoryId(categoryId);
    }

    @Override
    public List<Product> selectListByKeyword(String keyword, int page, int pageSize) {
        // 验证分页参数
        if (page < 1) {
            page = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        
        int offset = (page - 1) * pageSize;
        return productMapper.selectListByKeyword(keyword, offset, pageSize);
    }

    @Override
    public List<Product> selectListByCategory(Long categoryId, int page, int pageSize) {
        // 验证分页参数
        if (page < 1) {
            page = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        
        int offset = (page - 1) * pageSize;
        return productMapper.selectListByCategory(categoryId, offset, pageSize);
    }

    @Override
    public Product selectLatestProduct() {
        return productMapper.selectLatestProduct();
    }
} 